/*
Vangle: the javascript framework
name: Core
description: The heart of MooTools.
license: MIT-style license.
authors: bilipan
*/

var Error = function(msg) {
	alert(msg);
	return false;
};

// Class
// construct_arguments为初始添加的属性或操作
var Class = function(members_args) {
	var _self = arguments[0];
	var _superClass;
		
	// 默认的初始构造函数
	var _construct = function() {
		alert("construct");
		//cls.parent(_superClass);
		//_parent(_superClass, cls, arguments);
	};
	
	// 单继承
	var _extend = function(parent, child) {
		for(var index in parent.prototype) {
			if(index != "initialize") {
				if(!child.prototype[index])
					child.prototype[index] = parent.prototype[index];
			}
		}
	}
		
	var _implement = function(Interface) {
		var _interface = Interface;
		_interface.ensure(cls);
	};
	
	var _parent = function() {
		if(!arguments[0] || 
			arguments[0] == null || 
			typeof arguments[0] == "undefined"
		) {
			return false;
		}
		var parent = arguments[0];
		var child = arguments[1];
		var args = [];
		
		//arguments[2]为parent()内的数组参数
 		for(var i = 0; i < arguments[2].length; i++) {
			args.push(arguments[2][i]);
		}
		parent.prototype.initialize.apply(_self, args);
	}
		
	// 返回一个类本身
	var cls = function() {
/* 
		如果传入的构造参数为空或是没有自定义初始化函
		数或是自定义的变量initialize不是初始化函数，则调
		用默认的初始构造函数_self._construct()。
 */		 
 		if(/* arguments[0] == null ||  */
			!_self.initialize || 
			typeof _self.initialize != 'function'
		){
			_construct();
		}else{
			_self.initialize.apply(_self, arguments);
		}
 	}

	cls.prototype = members_args;
	
	// todo: 将遍历过程改为if...in...
	for(var index in members_args) {
		if(index == "privates") {}
		if(index == "extend") {
			_superClass = members_args[index];
			_extend(_superClass, cls);
		}
/*
	类实现接口时，如果该类有继承其它类，则先实现继承过
	程，再实现接口过程。因为若先实现接口，则无法使用继承
	过来的方法和属性。
*/
		if(index == "implement") {
			var _baseClass = "extend";
			if(_baseClass in members_args) {
				_superClass = members_args[_baseClass];
				_extend(members_args[_baseClass], cls);
			}
			for(var i = 0; i < members_args[index].length; i++) {
				var _interface = members_args[index][i];
				_implement(_interface);
			}
		}
	}
	
/*
	每个类都要显示地写明构造方法，若是存在子类，则要在其
	构造方法中显示地初始化化父类的构造方法。
*/
	cls.prototype.parent = function() {
		if(arguments[0]) {
			_parent(_superClass, cls, arguments);
		}
		var _superClass_properties = function() {};
		for(var i in _superClass.prototype) {
			_superClass_properties.prototype[i] = _superClass.prototype[i];
		}
		cls.prototype.parents = _superClass_properties.prototype;
	}
	
	cls.constructor = Class;
	
	// 为类本身添加单个自定义函数或属性
 	var _addProperty = function(member, value) {
		cls.prototype[member] = value;
	};
	
	// 解除单个自定义函数或属性
  	var _removeProperty = function(member) {
		delete cls.prototype[member];
	};
	
 	// 为类本身添加多个自定义函数或属性
	cls.bind = function(members) {
		for(index in members) {
			_addProperty.call(cls, index, members[index]);
		}
	};
	
	// 动态解除绑定在类上的多个方法或属性
 	cls.unbind = function(members) {
		for(var i = 0; i < members.length; i++) {
			_removeProperty.call(cls, members[i]);
		}
	};
	
	return cls;
};



//Interface
var Interface = function(properties) {
	var methods = properties;
	return {
		methods: methods,
 		ensure: function(class_arg) {
			for(var i = 0; i < this.methods.length; i++) {
				var method = class_arg.prototype[methods[i]];
				if(!method || method == 'undefined') {
					throw new Error("没有实现接口"+methods[i]);
					return false;
				}
			}
		},
 		extend:function(more_methods) {
			if(!arguments[0] || typeof arguments[0] != 'object') {
				throw new Error("接口继承参数类型错误");
				return false;
			}
			var method_ret = [];
			for(var i = 0; i < this.methods.length; i++) {
				method_ret.push(this.methods[i]);
			}
			for(var i = 0; i < more_methods.length; i++) {
				method_ret.push(more_methods[i]);
			}
			return new Interface(method_ret);
		}
	}
};

